#!/usr/bin/env python3

import os
import sys
import json

from download import download


def check(idx):
    print("(info) Checking %s..." % idx)

    try:
        folder = "./cogs/{}/".format(idx)
        files = os.listdir(folder)
        config = None
        for file in files:
            if file.endswith("json"):
                config = file
                break

        info = None
        with open(os.path.join(folder, config), "rb") as file:
            info = json.loads(file.read().decode("utf-8", "ignore"))

        name = info["name"]
        number = info["end_id"]

        if not os.path.exists("./cogs/{}/{}{}.out".format(idx, name, number)):
            download(idx)

    except Exception as e:
        print("(error) Error when checking {}: {}".format(idx, str(e)))


if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("Usage: %s [START] [END]" % sys.argv[0])
        exit(-1)

    start = int(sys.argv[1])
    end = int(sys.argv[2])

    for idx in range(start, end + 1):
        check(idx)
